﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 帮手连接
    /// </summary>
   public class HKHelper
    {
        #region 获取连接
        /// <summary>
        /// 获取连接
        /// </summary>
        /// <returns>OleDbConnection</returns>
        public static MySqlConnection GetConn()
        {
            //SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=ShopCommunity; Integrated Security=True;");
            //MySqlConnection conn = new MySqlConnection("Database='members_system';Data Source='sh-cynosdbmysql-grp-l6gz93lo.sql.tencentcdb.com';Port='25488';User Id='fubi';Password='an8tDuYKTJI%&!';charset='utf8';pooling=true;");
            MySqlConnection conn = new MySqlConnection("server=110.41.131.161;port=3306;user=helper;password=4w3NBFNCjnkZFss4; database=helper;charset='utf8';pooling=true;");
            //MySqlConnection conn = new MySqlConnection("server=110.41.131.161;port=3306;user=fubi_tests;password=ReLiPrhMrHS78RHZ; database=fubi_tests;charset='utf8';pooling=true;");
            return conn;
        }
        #endregion
        #region 增删改
        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql">执行语句</param>
        /// <param name="values">数组参数（OleDbParameter)</param>
        /// <returns>int</returns>
        public static int ExcutUpdate(string sql, MySqlParameter[] values)
        {
            // SqlConnection conn = GetConn();
            MySqlConnection conn = GetConn();
            conn.Open();

            MySqlCommand cmd = new MySqlCommand(sql, conn);

            if (values != null)
            {
                cmd.Parameters.AddRange(values);
            }

            int result = cmd.ExecuteNonQuery();
            conn.Close();
            return result;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sql">执行语句</param>
        /// <param name="values">数组参数（OleDbParameter)</param>
        /// <returns>OleDbDataReader</returns>
        public static MySqlDataReader ExcutQuery(string sql, MySqlParameter[] values)
        {

            MySqlConnection conn = GetConn();
            if (conn.State != ConnectionState.Open)
                conn.Open();

            MySqlCommand cmd = new MySqlCommand(sql, conn);

            if (values != null)
            {
                cmd.Parameters.AddRange(values);
            }
            MySqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            return rd;
        }
        #endregion
    }
}
